﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CorsatAdminApp.Library.TempTaskClasses;

namespace CorsatAdminApp.Desktop.UserControls
{
    public partial class ucItemsAdmin : UserControl
    {

        public Item CurrentItem = new Item();
        public bool DataChanged = false;

        public ucItemsAdmin()
        {
            InitializeComponent();
        }

        private void ucItemsAdmin_Load(object sender, EventArgs e)
        {
            List<CommonItem> CategoriesList = CurrentItem.GetAllCategories();
            if (CategoriesList != null)
            {
                lstCategories.DataSource = CategoriesList;
                lstCategories.DisplayMember = "ItemName";
                lstCategories.ValueMember = "ItemId";
            }
            else
            {
                MessageBox.Show("An Error occured!\nPlease contact Joseph Magdy :P", "Corsat Application", MessageBoxButtons.OK, MessageBoxIcon.Information);
                pnlCategories.Enabled = false;
                pnlItems.Enabled = false;
                pnlDetails.Enabled = false;
            }
        }

        private void lstCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstCategories.SelectedItem != null)
            {
                lblMainItemType.Text = ((CommonItem)lstCategories.SelectedItem).ItemName;
                List<Item> ItemsList = CurrentItem.GetCategoryItems(((CommonItem)lstCategories.SelectedItem).ItemId);
                if (ItemsList != null)
                {
                    lstItems.DataSource = ItemsList;
                    lstItems.DisplayMember = "ItemName";
                    lstItems.ValueMember = "ItemId";
                }
                else
                {
                    lstItems.DataSource = null;
                    txtItemName.Text = "";
                    txtItemDescription.Text = "";
                }
            }

        }

        private void lstItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstItems.SelectedItem != null)
            {
                CurrentItem = (Item)lstItems.SelectedItem;

                txtItemName.Text = CurrentItem.ItemName;
                txtItemDescription.Text = CurrentItem.ItemDescription;
                if (CurrentItem.ParentItem.HasValue)
                    lnkParentItem.Text = CurrentItem.ParentItem.Value.ItemName;
                else
                    lnkParentItem.Text = "حدد";

            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CurrentItem != null)
            {
                CurrentItem.ItemName = txtItemName.Text;
                CurrentItem.ItemDescription = txtItemDescription.Text;
                
                CurrentItem.Save();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (CurrentItem != null)
                CurrentItem.Delete();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            CurrentItem = new Item();
            CommonItem currentCategory = new CommonItem();
            currentCategory.ItemId = ((CommonItem)lstCategories.SelectedItem).ItemId;
            currentCategory.ItemName = ((CommonItem)lstCategories.SelectedItem).ItemName;

            CurrentItem.ItemCategory = currentCategory;

            txtItemDescription.Text = "";
            txtItemName.Text = "";
            lnkParentItem.Text = "حدد";
        }
    }
}
